layui.use(['laydate','form','layer'],function(){
    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var laydate = layui.laydate;
    laydate.render({
        elem: '#startTime', //指定元素
        type: 'datetime'
    });
    laydate.render({
        elem: '#endTime', //指定元素
        type: 'datetime'
    });

    form.on("submit(btnSubmit)",function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        var field = data.field;
        var type = field.type;
        // 获取值
        var memberIdArr = [];
        $("#memberList").find(".xyg-form-btn").each(function(_index,_element){
            var _val = $(_element).attr("data-value");
            if($.trim(_val).length>0){
                memberIdArr.push(_val);
            }
        });
        var memberIds="";
        if(memberIdArr.length> 0){
            memberIds = memberIdArr.join(",");
        }
        // 实际使用时的提交信息
        $.post("/coupon/updateOrAddEntity",{
            fullMoney : $("#fullMoney").val(),
            reduceMoney : $("#reduceMoney").val(),
            startTime : $("#startTime").val(),
            endTime : $("#endTime").val(),
            // description : $("#description").val(),
            type:type,
            memberIds:memberIds
        },function(result){
            if(1==result.returnInfo.state){
                top.layer.close(index);
                top.layer.msg("操作成功！");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            }else{
                top.layer.msg(result.returnInfo.errorMsg);
            }
        },"json")
        return false;
    });

    form.verify({
        money: function(value, item) { //value：表单的值、item：表单的DOM对象
            var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
            if (!reg.test(value)) {
                return '金额的输入格式不正确,请确认!';
            }
        }
    });


    form.on('radio(type)', function (data) {
        if(data.value=="2"){
            $(".xyg-param").show();
        }else{
            $(".xyg-param").hide();
        }
    });

    $("#paramContentBtn").click(function(){
        // 算出已有的用户id
        var memberIdArr = [];
        $("#memberList").find(".xyg-form-btn").each(function(_index,_element){
            var _val = $(_element).attr("data-value");
            if($.trim(_val).length>0){
                memberIdArr.push(_val);
            }
        });
        var memberIds="";
        if(memberIdArr.length> 0){
            memberIds = memberIdArr.join(",");
        }
        var index = layui.layer.open({
            title :"选择商品",
            type : 2,
            content : "/member/listSelectPage?memberIds="+memberIds,
            area:['750px','400px'],
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                })
            },
            end: function () {
                // 选择
            }
        });
    });
})